//
//  HDSinglePickerView.m
//  AccountEasy
//
//  Created by 黄志刚 on 2020/8/14.
//  Copyright © 2020 AccountEasy. All rights reserved.
//

#import "HDSinglePickerView.h"
#import "NSArray+HDsafe.h"

@interface HDSinglePickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
// pickerview  创建
@property (nonatomic ,strong)UIView *toolsView;
@property (nonatomic ,strong)UIPickerView *pickerView;
@property (nonatomic ,strong)NSArray *contentArray;
@end

@implementation HDSinglePickerView

/**
 初始化选择器
 @param title 选择器标题
 @param contentArray 数据
 */
- (instancetype)initWithTitle:(NSString *)title  contentArray:(NSArray *)contentArray{
    
    if (self = [super init]){
        self.contentArray = contentArray;
        self.titleString = title;
        self.selectedRow = 0;
        self.selectedString = [self.contentArray hd_safeObjectAtIndex:self.selectedRow];
        [self setupUI];
    }
    return self;
}
/**
 初始化选择器
 @param title 选择器标题
 @param contentArray 数据
 @param selectedIndex 刚进来就选中哪一行
 */
- (instancetype)initWithTitle:(NSString *)title  contentArray:(NSArray *)contentArray selectedIndex:(NSUInteger)selectedIndex{
    if (self = [super init]){
        self.contentArray = contentArray;
        self.titleString = title;
        self.selectedRow = selectedIndex;
        [self setupUI];
        self.selectedString = [self.contentArray hd_safeObjectAtIndex:selectedIndex];
        [_pickerView selectRow:selectedIndex inComponent:0 animated:YES];
        
    }
    return self;
}

- (void)setupUI{
    
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    self.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    }];
    
    _toolsView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(250), kScreenWidth, kScaleNum(55))];
    _toolsView.backgroundColor = colorHex(0xF7F8F9);
    _toolsView.layer.cornerRadius = 5;
    _toolsView.layer.masksToBounds = YES;
    [self addSubview:_toolsView];
    
    // 右边确定按钮
    UIButton *rightSureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightSureBtn.frame = SARectMake(kBaseScreenWidth - 60, 0, 60, 44);
    [rightSureBtn setTitle:@"确定" forState:UIControlStateNormal];
    rightSureBtn.titleLabel.font = kFont(15);
    [rightSureBtn setTitleColor:colorHex(0x3B9BFF) forState:UIControlStateNormal];
    [rightSureBtn addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_toolsView addSubview:rightSureBtn];
    
    // 中间显示  label
    if (self.titleString.length > 0) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:SARectMake(54, 0, kBaseScreenWidth - 108, 44)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = self.titleString;
        titleLabel.font = [UIFont systemFontOfSize:16];
        [_toolsView addSubview:titleLabel];
    }
     
    // 左边取消按钮
    UIButton *leftCancleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftCancleButton.frame = CGRectMake(0, 0, 60, 44);
    [leftCancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [leftCancleButton setTitleColor:colorHex(0xA5AEB7) forState:UIControlStateNormal];
    leftCancleButton.titleLabel.font = kFont(15);
    [leftCancleButton addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_toolsView addSubview:leftCancleButton];
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(202), kScreenWidth, kScaleNum(202))];
    _pickerView.dataSource = self;
    _pickerView.delegate = self;
    [_pickerView selectRow:0 inComponent:0 animated:YES];
    _pickerView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_pickerView];
}
#pragma mark -  左边按钮 方法  取消
- (void)leftButtonClick:(UIButton *)button{
    [self dismiss];
}

#pragma mark -  右边按钮  方法
- (void)rightButtonClick:(UIButton *)button{
    if (self.getPickerValue){
        self.getPickerValue(self.selectedRow,self.selectedString);
    }
    [self dismiss];
}


#pragma mark -  数据源方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.contentArray.count;

}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return kScaleNum(44);
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.contentArray hd_safeObjectAtIndex:row];
}
//重写方法
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:kFont(15)];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
    
}

#pragma mark -  代理方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.selectedRow = row;
    self.selectedString = [self.contentArray hd_safeObjectAtIndex:row];
}

#pragma mark - 屏幕点击事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}
/**
 显示PickerView
 */
- (void)show{
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
}

#pragma mark - 消失的方法
- (void)dismiss
{
    __weak typeof (self)weakSelf = self;
    __weak typeof(UIView *)blockView = _toolsView;
    __weak typeof(UIPickerView *)blockPickerViwe = _pickerView;
    [UIView animateWithDuration:0.3 animations:^{
        blockView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScaleNum(55));
        blockPickerViwe.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScaleNum(200));
    }completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

@end
